package com.markyao.recos.get;

import com.markyao.service.redis.UtilsKeys;
import com.markyao.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j(topic = "m.RecosGetServiceImpl")
@Service
public class RecosGetServiceImpl implements RecosGetService{
    @Autowired
    @Qualifier("connectSet")
    Map<String, Set<String>> connectSet;
    @Autowired
    @Qualifier("providersMap")
    Map<String,Map<String,String>>providersMap;
    @Autowired
    @Qualifier("refrenceRegistrySet")
    Map<String,Set<String>>refrenceRegistrySet;
    @Autowired
    @Qualifier("serverRegistryMap")
    Map<String,Map<String,String>>serverRegistryMap;
    @Autowired
    @Qualifier("serviceRegistryMap")
    Map<String,Map<String,String>>serviceRegistryMap;
    @Autowired
    @Qualifier("urlMap")
    Map<String,String>urlMap;
    @Autowired
    @Qualifier("heartBeatMap")
    Map<String,Long>heartBeatMap;
    @Autowired
    @Qualifier("errorUrlMap")
    Map<String,String>errorUrlMap;
    @Autowired
    @Qualifier("serverKeySet")
    Set<String>serverKeySet;

    @Override
    public Result getAppName(String url) {
        String appName = urlMap.get(url);
        return Result.success("获取成功",appName);
    }

    @Override
    public Result getWeight(String appName) {
        Map<String, String> map = serverRegistryMap.get(appName);
        String weight = map.get(UtilsKeys.WEIGHT);
        return Result.success("获取成功",weight);
    }

    @Override
    public Result getProviderCount(String interfaceNAME) {
        Map<String, String> map = providersMap.get(interfaceNAME);
        int size=map.size();
        return Result.success("获取成功",size);
    }

    @Override
    public Result getUrlForWeights(String interfaceNAME) {
        Map<String, String> map = providersMap.get(interfaceNAME);
        return Result.success(map);
    }

    @Override
    public Result getUrlStrSeleceds(String interfaceNAME) {
        return null;
    }

    @Override
    public Result getProviderUrlStrByIntefaceName(String interfaceName) {
        Map<String, String> map = providersMap.get(interfaceName);
        Set<String> set = map.keySet();

        return Result.success(set.toArray(new String[]{}));
    }

    @Override
    public Result getInitConnects(String appName) {
        Set<String>initSet=new HashSet<>();
        if (refrenceRegistrySet.containsKey(appName)){
            Set<String> set = refrenceRegistrySet.get(appName);
            for (String refrenceName : set) {
                Map<String, String> map = providersMap.get(refrenceName);
                if (map==null){
                    return Result.failed(499,"无提供者列表~");
                }
                for (String key : map.keySet()) {
                    initSet.add(key);
                }
            }
            return Result.success(initSet.stream().collect(Collectors.toList()));
        }
//        log.error("无提供者列表！");
        return Result.success(Collections.emptyList());
    }

    @Override
    public Result hasKey(String k) {
        boolean f = serverKeySet.contains(k);
        return Result.success(f);
    }

    @Override
    public Result getUrlByAppName(String appName) {
        String url = serverRegistryMap.get(appName).get(UtilsKeys.URL);
        return Result.success("get success",url);
    }


}
